package com.hanock.sm.activity;


import com.hanock.sm.app.Application;
import com.hanock.sm.app.GlobalState;
import com.hanock.sm.fork2d.Fork2dView;
import com.hanock.sm.fork2d.Scene;
import com.hanock.sm.fork2d.SceneManager;
import com.hanock.sm.scene.TestScene;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;


public class MainActivity extends AbstractActivity{


	
	
	private Fork2dView forkView;
	private SceneManager sceneManager;
	private Application application;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        System.gc();

        forkView = new Fork2dView(getApplicationContext());
        sceneManager = SceneManager.getInstance();
        
        application = Application.getInstance();
        application.initialize(this, handler);
        
        application.getSoundManager().load(getApplicationContext());
        
        setContentView(forkView);
    }
    
    @Override
    public void onContentChanged() {
    	super.onContentChanged();
  
    	forkView.startScene(new TestScene(), 35);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	return true;
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
    	
    	menu.clear();
    	
    	Scene scene = sceneManager.getCurrentScene();
    	if (scene == null)
    		return true;
    	
    	Class<?> clazz = scene.getClass();
    	menu.add(0, 0, 0, "");
    	
    	
    	return true;
    }
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		forkView.onMenu(item.getItemId());
		return true;
	}
	
	@Override
	public void onBackPressed() {
		
		//Scene scene = sceneManager.getCurrentScene();
    	//if (scene == null || forkView == null)
    		super.onBackPressed();
    	//else
		//	forkView.onBackPressed();
	}
	
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		
		if (hasFocus && !GlobalState.pause){
			GlobalState.background = false;
			if (application != null && application.isInitialized())
				application.getSoundManager().playBackground();
		}
	}
	
	@Override
    protected void onPause() {
    	super.onPause();
    	forkView.onPause();
    	GlobalState.background = true;
    	GlobalState.pause = true;
    	
    	application.getSoundManager().pauseBackground();
    	application.getSoundManager().pauseLoopSound();
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    	forkView.onResume();
    	GlobalState.pause = false;
    }
    
    @Override
    protected void onStart() {
    	super.onStart();
    	
    	GlobalState.background = false;
    	GlobalState.pause = false;
    }
    
    @Override
    protected void onDestroy() {
    	super.onDestroy();
    	forkView.onDestroy();
    }
    
    private Handler handler = new Handler(){
    	
    	public void handleMessage(Message msg) {

    	};
    };


	
}










